<?php
/**
 * 授权管理
 * 当前发者开发一个新功能时，通需要添加一个授权标识
 * 在执行某个操作之前，使用$this->checkPermission(标识)来验证授权
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Category;
use Common\Library\Page;
class PermissionController extends CommonController{
	private $msg=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=D('Permission');
	}
	public function index(){
		if(!$this->checkPermission('Permission')) $this->jump();
		$list=array();
		$title=I('get.title','','trim');
		$name=I('get.name','','trim');		
		$permissions=$this->Model->getPermissions();
		$permissions=Category::unlimitedForLevel($permissions,'',0,0,'permission_id','parent_id','title');
		$url=array(
			'title'	=> $title,
			'name'	=>	$name,
			'p'		=>	I('get.p','')
			);
		$filter=array(
			'title'		=>	$title,
			'name'		=>	$name
			);
		$temp=array();
		if($name){
			foreach ($permissions as $value) {
				if(strpos($value['name'],$name) !== false) $temp[]=$value;
			}
		}else{
			$temp=$permissions;
		}
		if($title){
			foreach ($temp as $v) {
				if(strpos($v['title'],$title) !== false) $list[]=$v;
			}
		}else{
			$list=$temp;
		}
		$count=count($list);
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$show=$Page->show();
		$list=array_splice($list,$Page->firstRow,$Page->listRows);
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('filter',$filter);
		$this->assign('page',$show);
		$this->display();
	}
	public function oper($permission_id=0){
		if(!$permission_id && !$this->checkPermission('AddPermission')) $this->jump();
		//不能选择自身作为父级，所以需要排除
		if($permission_id) $parent_filter['permission_id']=array('neq',$permission_id);
		$parent_filter['parent_id']=0;
		$parents=$this->Model->getPermissions($parent_filter);
		$permission=$this->Model->getPermission($permission_id);
		if($permission_id && empty($permission)) $this->jump(L('error_404'));
		if(IS_POST){
			$permission=I('post.');
			if($this->validateForm($permission_id)){
				$this->Model->operPermission($permission,$permission_id);
				session('success',$permission_id?L('success_edit'):L('success_add'));
				$url=array(
					'title'	=> I('get.title','','trim'),
					'name'	=>	I('get.name','','trim'),
					'p'		=>	I('get.p','')
					);
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$this->assign('parents',$parents);
		$this->assign('msg',$this->msg);
		$this->assign('languages',get_langs());
		$this->assign('permission',$permission);
		$this->display();
	}

	public function validateForm($permission_id=0){
		if($permission_id && !$this->checkPermission('EditPermission')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		if(mb_strlen($data['name'])>64 || mb_strlen($data['name'])<2){
			$this->msg['name']=L('error_name');
		}else{
			$map=array('name'=>$data['name']);
			if($permission_id) $map['permission_id']=array('neq',$permission_id);
			$perm=M('Permission')->where($map)->find();
			if($perm) $this->msg['name']=L('error_name_exist');
		}
		foreach ($data['description'] as $language_id => $v) {
			if(mb_strlen($v['title'])>64||mb_strlen($v['title'])<2) $this->msg['title'][$language_id]=L('error_title');
		}
		return !$this->msg;
	}
	public function del(){
		if($this->checkPermission('DeletePermission')){
			$permission_ids=I('post.selected');
			$count=0;
			foreach ($permission_ids as $permission_id) {
				$map=array(
					'permission_id'		=> $permission_id,
					'parent_id'			=>	$permission_id,
					'_logic'			=>	'OR'
					);
				$count += M('Permission')->where($map)->delete();
			}
			session('success',L('success_delete',array('count'=>$count)));
		}else{
			session('danger',L('error_not_permission'));
		}
		$this->redirect('index');
	}
	
	public function autocomplete(){
		$json=array();
		$name=I('filter_name','','trim');
		$title=I('filter_title','','trim');
		$html='title';
		if(isset($_GET['filter_name'])) $html='name';
		$list=$this->Model->getPermissions();
		$list=Category::unlimitedForLevel($list,'',0,0,'permission_id','parent_id',$html);
		if($name || $title){
			foreach ($list as $value) {
				if((strpos($value['name'],$name) !== false) || (strpos($value['title'], $title) !== false)){
					$json[]=$value;
				}
			}
		}else{
			$json=$list;
		}		
		$json=array_splice($json,0,8);
		$this->ajaxReturn($json,'JSON');
	}
}